"use strict";
var stoneCache=new StoneGraphics();

function StoneGraphics()
{
    var i;
    this.canvas=null;
    this.cache=[];
    
    this.getStone=function(color,size)
    {
        var sid;
        sid=color+"_"+size;
        for(i=0;i<this.cache.length;i++)
            if(this.cache[i].id==sid)
                return this.cache[i];
                
        return null;
                
        /* this.createStone(color,size,null); */
    }
    
    this.createStone=function(color,ssize,obj)
    {
        var context,grad,radius,img,__this;       
        if(img!=null)
            return img;
            
        if(this.canavs==null)
        {
            this.canvas = document.createElement('canvas');
            if(!this.canvas.getContext)
                return null;
        }
        
        this.canvas.width  = ssize+2;
        this.canvas.height = ssize+2;
        context = this.canvas.getContext('2d');
        context.clearRect(0,0,this.canvas.width,this.canvas.height);
        radius=(ssize-1)/2;
        grad = context.createRadialGradient(radius/1.5, radius/1.5, 2,
            radius/1.5, radius/1.5, ssize/3);
            
        if(color=='white')
        {
            grad.addColorStop(0, "#FFFFFF");
            grad.addColorStop(1, "#DDDDDD");

        }
        else if(color=='black')
        {
            grad.addColorStop(0, "#808080");
            grad.addColorStop(1, "#000000");
        }
                
        context.beginPath();
        context.fillStyle = 'rgba(0, 0, 0, 0.5)';
        context.arc(radius+2, radius+2, radius, 0, 2 * Math.PI, false);
        context.fill();
        context.closePath();
        
        context.beginPath();
        context.fillStyle = grad;
        context.arc(radius, radius, radius-0.5, 0, 2 * Math.PI, false);
        context.fill();
        context.closePath();
        
        
        img=new Image();
        img.id=color+'_'+ssize;
        __this=this;
        img.onload=function()
        {            
            __this.cache.push(this);
            if(obj!=null)
            {
                //console.log("Image ready",color,"id: "+obj.m_config.index);
                obj.gobanImageReady();
            }
        };
        img.src=this.canvas.toDataURL('image/png');
    }
}

function SGoGoban(canvas)
{
    this.m_ctx=null;
    this.m_canvas=canvas;
    this.pointx=null;
    this.pointx=null;
    this.m_whiteImg=null;
    this.m_blackImg=null;
    this.m_stonesize=0;
    /* this.boardColor='rgb(197,179,88)'; */
    this.boardColor='rgb(221,179,92)';
    this.stonesready=0;
    this.m_stonecache=new Array();

    if (this.m_canvas.getContext)
    {
        this.m_ctx = this.m_canvas.getContext('2d');
    }
    else
    {
        console.error("Your browser has no support for canvas");
    }
    this.boardSize={width:0,height:0};

}

SGoGoban.prototype.resize=function(width, height, stonesize)
{
    var i;

    this.boardSize.width=width;
    this.boardSize.height=height;
    this.m_stonesize=stonesize;

    if(this.pointx==null)
        this.pointx=new Array(width);
    if(this.pointy==null)
        this.pointy=new Array(height);

    for(i=0;i<width;i++)
    {
        this.pointx[i]=(Math.round(this.m_stonesize+i*this.m_stonesize)+0.5);
    }
    for(i=0;i<height;i++)
    {
        this.pointy[i]=(Math.round(this.m_stonesize+i*this.m_stonesize)+0.5);
    }

    if(this.m_ctx==null)
        return false;
    
    this.m_ctx.textBaseline = "middle";
    this.m_ctx.textAlign    = "center";
    //this.m_ctx.font         = 'bold 12px sans-serif';

    return true;
}

SGoGoban.prototype.drawGrid=function(margins)
{
    var i,j,sx,sy,ex,ey;

    if(this.m_ctx==null)
        return false;

    this.m_ctx.fillStyle = this.boardColor;
    this.m_ctx.fillRect (0,0,this.m_canvas.width,this.m_canvas.height);

    sx=margins.top?0:1;
    ex=margins.bottom?this.boardSize.height:(this.boardSize.height-1);
    sy=margins.left?0:1;
    ey=margins.right?this.boardSize.width:(this.boardSize.width-1);
    
    this.m_ctx.strokeStyle = '#000000';    
    this.m_ctx.beginPath();
    
    for(i=sx;i<ex;i++)
    {        
        this.m_ctx.moveTo(this.pointx[0],this.pointy[i]);
        this.m_ctx.lineTo(this.pointx[this.boardSize.width-1],this.pointy[i]);

    }
    for(i=sy;i<ey;i++)
    {
        this.m_ctx.moveTo(this.pointx[i],this.pointy[0]);
        this.m_ctx.lineTo(this.pointx[i],this.pointy[this.boardSize.height-1]);

    }
    this.m_ctx.stroke();

    return true;
}

SGoGoban.prototype.drawCoordinates=function(bounds, margins)
{
    var i, ch, ch_i;
    
    this.m_ctx.fillStyle = "#333";    
    
    ch_i = 65 + bounds.left;
    for(i=0;i<this.boardSize.width;i++)
    {        
        if((ch_i+i)==73)
        {
            ch_i++;
        }
        ch = String.fromCharCode(ch_i+i);
        
        if(margins.top)
            this.m_ctx.fillText(ch, this.pointx[i], this.pointy[0]-12);  
        if(margins.bottom)
            this.m_ctx.fillText(ch, this.pointx[i], this.pointy[this.boardSize.height-1]+12);

    }
    
    ch_i = bounds.top;    
    for(i=0;i<this.boardSize.height;i++)
    {                        
        ch=" " +(bounds.bottom-(ch_i+i));
        if(margins.left)
            this.m_ctx.fillText(ch, this.pointx[0]-16, this.pointx[i]);
        if(margins.right)
            this.m_ctx.fillText(ch, this.pointx[this.boardSize.width-1]+10, this.pointy[i]);  
    }
}

SGoGoban.prototype.drawStone=function(color,x,y)
{
    var x1,y1;

    if(this.m_ctx==null)
        return false;

    if(color==SGFProp.COLOR_NONE)
        return;
    if((x<0) || (x >=this.pointx.length) || (y<0) || (y >=this.pointy.length))
    {
         console.warn("Invalid x,y coordinates");
         return;
    }

    x1=this.pointx[x]-this.m_stonesize/2;
    y1=this.pointy[y]-this.m_stonesize/2;

    if(color==SGFProp.COLOR_BLACK)
    {
        if(this.m_blackImg==null)
            this.m_blackImg=StoneCache.getStone('black',this.m_stonesize);
            
        if(this.m_blackImg!=null)
            this.m_ctx.drawImage(this.m_blackImg,x1,y1);
            
    }
    else if(color==SGFProp.COLOR_WHITE)
    {
        if(this.m_whiteImg==null)
            this.m_whiteImg=StoneCache.getStone('white',this.m_stonesize);
                    
        if(this.m_whiteImg!=null)
            this.m_ctx.drawImage(this.m_whiteImg,x1,y1);
    }    
    return true;
}


SGoGoban.prototype.drawTriangle=function(color,x,y)
{
    
    this.m_ctx.strokeStyle = color;
        
    var r=this.m_stonesize/2;
    var height = r * (Math.sqrt(3)/2);
    x=this.pointx[x];
    y=this.pointy[y];
    y=y-r/2;     
    this.m_ctx.beginPath();
    this.m_ctx.moveTo(x, y);
    this.m_ctx.lineTo(x+r/2, Math.round(y+height));
    this.m_ctx.lineTo(x-r/2, Math.round(y+height));
    this.m_ctx.lineTo(x, y);
    this.m_ctx.stroke();
    this.m_ctx.closePath();
    
}

SGoGoban.prototype.drawText=function(color,txt,x,y)
{
    if(this.m_ctx==null)
        return false;
    
    this.m_ctx.fillStyle = color;

    if(this.m_ctx.fillText)
    {
        this.m_ctx.fillText(txt, this.pointx[x], this.pointy[y]);
    }
    return true;
}

SGoGoban.prototype.drawHandi=function(x,y)
{
    if(this.m_ctx==null)
        return false;

    this.m_ctx.fillStyle = '#000';
    this.m_ctx.beginPath();
    this.m_ctx.arc(this.pointx[x],this.pointx[y], 2.5, 0, Math.PI*2, false);
    this.m_ctx.fill();
    this.m_ctx.closePath();

    return true;
}

SGoGoban.prototype.clearRect=function(x,y)
{
    var ssize=this.m_stonesize/2;
    this.m_ctx.fillStyle=this.boardColor;
    this.m_ctx.beginPath();
    this.m_ctx.arc(this.pointx[x],this.pointx[y], this.m_stonesize/2, 0, Math.PI*2, false);
    this.m_ctx.fill();
    this.m_ctx.closePath();
}

/*private function for fransofrm mouse point to goban points */

SGoGoban.prototype.__getPointAt=function(x, width, parr)
{
    var i, pnt;
    pnt=-1;
    if(x<parr[0]) 
        pnt = 0;
    else if( x > parr[width-1]) 
        pnt = width-1;
    else
    {
        for(i=0;i<width;i++)
        {   
            if( (x >= parr[i]) && (x <= parr[i+1]))
            {
                if((x - parr[i]) < (parr[i+1]-x))
                    pnt=i;
                else
                    pnt=i+1;
                break;
            }
        }
    }
    return pnt;
}

SGoGoban.prototype.getPointAt=function(x,y)
{
   var pos={x:-1, y:-1};
   pos.x=this.__getPointAt(x,this.boardSize.width,this.pointx);
   pos.y=this.__getPointAt(y,this.boardSize.height,this.pointy);
   
   return pos;
}


